/**
 * @(#)RssParser.java, 2010-5-14. 
 * 
 * Copyright 2010 Youdao, Inc. All rights reserved.
 * YOUDAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.rsstuan.parser;

import java.io.StringReader;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Logger;

import com.rsstuan.data.FeedOut;
import com.rsstuan.storage.data.TuanGouItemData;
import com.rsstuan.utils.ItemUtils;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;

/**
 *
 * @author guosq
 *
 */
public class RssParser {
    private final static Logger LOG = Logger.getLogger(RssParser.class.getName());

    
    public static List<TuanGouItemData> parseToTuanGouItems(String str, String url) throws IllegalArgumentException, FeedException {
	    SyndFeedInput input = new SyndFeedInput();
		SyndFeed feed = input.build(new StringReader(str));
		
		List<TuanGouItemData> items = new ArrayList<TuanGouItemData>();
		List entries = feed.getEntries();
		for (int i = 0; i < entries.size(); i ++) {
			SyndEntryImpl entry = (SyndEntryImpl)entries.get(i);
			TuanGouItemData item = new TuanGouItemData();
			item.setTitle(entry.getTitle());
			if (entry.getPublishedDate() != null ) {
				item.setPubTime(entry.getPublishedDate().getTime());
			}
			if (entry.getDescription() != null ) {
				item.setDescription(entry.getDescription().getValue());
				item.setImage(ItemUtils.getImageUrl(item.getDescription()));
			}
			item.setLink(entry.getLink());
			if (item.getTitle() != null) {
				item.setOrigPrice(ItemUtils.getOrigPrice(item.getTitle()));
				item.setPrice(ItemUtils.getPrice(item.getTitle()));
			}
			items.add(item);
			
		}
		return items;
    }
	
}
